var teamEmailClient = {
  
  init: function() {
    
    var form = $('#send-email');
    
    $('#result').show();

    form.find('#recipients_all').click(function() {
      if (this.checked) {
        form.find('.recipient_individual').removeAttr('checked');
      }
    });
    
    form.find('.recipient_individual').click(function() {
      if (this.checked) {
        form.find('#recipients_all').removeAttr('checked');
      }
    });    

    // Remove error message when the associated field is focused
    form.find('.required').focus(function() {
      teamEmailClient.getErrorEl(this).hide();  
    });    
    form.find('input[name=recipients]').click(function() {
      $('#errorMessage_recipients').hide();
    });
    
    $('#insert-pad-link').click(function() {
      $('#message').insertAtCaret(clientVars.siteLink);
    })
    
    $('#send-email').submit(function() {
      $('#result').hide();
      return teamEmailClient.validateInput();
    });   
    
    $('input[name=cancel]').click(function() {
      teamEmailClient.clearForm();
    })
  },

  // Block submission with empty fields. Doing quick and dirty JavaScript
  // validation to avoid complications of getting data back into form
  // after failing server-side validation. Nothing goes in the db, so
  // we don't need to be strict about server-side validation.  
  validateInput: function() {
    var hasErrors = false,
        form = $('#send-email'),
        checkboxes = form.find('.checkboxes');
    
    $('#form-error').hide();
    form.find('.field-error').hide();
    
    form.find('.required').each(function(){
      var el = $(this),
          error = teamEmailClient.getErrorEl(el);
      
      if ( ! el.val() ) {
        hasErrors = true;
        error.show();
      }      
    });
    
    if ( ! checkboxes.find('input[type=checkbox]:checked').length) {
      checkboxes.find('#errorMessage_recipients').show();
    }
    
    if (hasErrors) {
      $('#form-error').show();
      
    }
    return ! hasErrors;  
  },
  
  getErrorEl: function(el) {
    if (! ( el instanceof jQuery ) ) {
      el = $(el);
    }
    return $('#errorMessage_' + el.attr('name'));
  },
  
  clearForm: function() {
    var form = $('#send-email');
    
    form.find('input[type=text], textarea').val('');
    form.find('input[type=checkbox]').removeAttr('checked');
    form.find('.field-error').hide();
    $('#form-error').hide();
  }
  
};

$(document).ready(function() {
  teamEmailClient.init();
});

$.fn.extend({
  insertAtCaret: function(myValue) {
    
    // selectionStart / selectionEnd are defined on DOM elements, not jQuery objects
    var textarea = this[0],
        startPos,
        endPos,
        scrollTop,
        before,
        after, 
        sel;
        
    if (typeof textarea.selectionStart !== 'undefined') {
        startPos = textarea.selectionStart;
        endPos = textarea.selectionEnd;
        scrollTop = textarea.scrollTop;
        before = textarea.value.substring(0, startPos);
        after = textarea.value.substring(endPos,textarea.value.length);
        if (before.length && ! / $/.test(before)) { 
          myValue = ' ' + myValue;
        }
        if (after.length && ! /^ /.test(after)) {
          myValue += ' ';
        }
        textarea.value = before + myValue + after;
        textarea.focus();
        textarea.selectionStart = startPos + myValue.length;
        textarea.selectionEnd = startPos + myValue.length;
        textarea.scrollTop = scrollTop;
     
    // selectionStart / selectionEnd not defined in IE 8 and below   
    } else if (document.selection) {
      this.focus();
      sel = document.selection.createRange();
      sel.text = myValue;
      this.focus();    
   
    } else {
      this.value += myValue;
      this.focus();
    }
  }
});
